package kr.hkit.androidcomplete01.chapter08;

import kr.hkit.androidcomplete01.chapter08.HandlerEvent.MyView;
import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnLongClickListener;

public class MoveCircle extends Activity {
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		final MyView vw = new MyView(this);
		vw.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				if(vw.mColor == Color.RED){
					vw.mColor = Color.BLUE;
				}else{
					vw.mColor = Color.RED;
				}
				return true;
			}
		});
	}
	
	private MyView vw;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		vw = new MyView(this);
		vw.setFocusable(true);
		vw.setFocusableInTouchMode(true);
		setContentView(vw);
	}

	protected class MyView extends View {
		float mX, mY;
		int mColor;

		public MyView(Context context) {
			super(context);
			mX = 100;
			mY = 100;
			mColor = Color.BLACK;
		}

		public void onDraw(Canvas canvas) {
			canvas.drawColor(Color.LTGRAY);
			Paint Pnt = new Paint();
			Pnt.setColor(mColor);
			Pnt.setAntiAlias(true);
			canvas.drawCircle(mX, mY, 16, Pnt);
		}

		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			super.onKeyDown(keyCode, event);
			if (event.getAction() == KeyEvent.ACTION_DOWN) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_DPAD_LEFT:
					mX -= 5;
					invalidate();
					return true;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					mX += 5;
					invalidate();
					return true;
				case KeyEvent.KEYCODE_DPAD_UP:
					mY -= 5;
					invalidate();
					return true;
				case KeyEvent.KEYCODE_DPAD_DOWN:
					mY +=5;
					invalidate();
					return true; 
				case KeyEvent.KEYCODE_DPAD_CENTER:
					if (mColor == Color.BLUE) {
						mColor = Color.RED;
					} else {
						mColor = Color.BLUE;
					}
					invalidate();
					return true;

				}
			}
			return false;
		}
	}
}
